package android;

import hibernate.Driverstab;
import hibernate.DriverstabDAO;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Servlet implementation class RegistServlet
 */

public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegistServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**9
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");


		JSONObject jsonObj=null;
		String reciveAccount=null;
		String recivePwd=null;
		String reciveLicence=null;
		String reciveName=null;
		String reciveTel=null;
		String reciveEmail=null;

		try {
			jsonObj=new JSONObject(request.getParameter("jsonString"));
			reciveAccount=jsonObj.getString("account");
			recivePwd=jsonObj.getString("pwd");
			reciveLicence=jsonObj.getString("licence");
			reciveName=jsonObj.getString("name");
			reciveTel=jsonObj.getString("tel");
			reciveEmail=jsonObj.getString("email");
			
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		System.out.println("RegistServlet get account  "+reciveAccount+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

		JSONObject resultJson=new JSONObject();
		boolean noError=true;
		
		//��֤�ֻ�� 11λ����
		String telPatternString="[0-9]{11}";
		Pattern telPattern=Pattern.compile(telPatternString);
		if(noError&&!telPattern.matcher(reciveTel).matches())
		{
			noError=false;
			try {
				resultJson.put("registResult","fail");
				resultJson.put("errorPoint","telInvalid");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//��֤���ƺ�
		String licencePatternString="[京][A-Z][A-F0-9]{5}";
		Pattern licencePattern=Pattern.compile(licencePatternString);
		if(noError&&!licencePattern.matcher(reciveLicence).matches())
		{
			noError=false;
			try {
				resultJson.put("registResult","fail");
				resultJson.put("errorPoint","licenceInvalid");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//��֤����
		
		String pwdPatternString="[0-9A-Za-z]{8,20}";
		Pattern pwdPattern=Pattern.compile(pwdPatternString);
		if(noError&&!pwdPattern.matcher(recivePwd).matches())
		{
			noError=false;
			try {
				resultJson.put("registResult","fail");
				resultJson.put("errorPoint","pwdInvalid");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		//��֤�û��� ����޴�����ݿ�����û�
		
		String accountPatternString="[0-9A-Za-z]{4,20}";
		Pattern accountPattern=Pattern.compile(accountPatternString);
		if(noError&&!accountPattern.matcher(reciveAccount).matches())
		{
			noError=false;
			try {
				resultJson.put("registResult","fail");
				resultJson.put("errorPoint","accountInvalid");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(noError)
		{
			List<Driverstab> driversList=null;
			DriverstabDAO driverstabDAO=new DriverstabDAO();
			driverstabDAO.getSession().beginTransaction();
			try
			{

				String hql="from Driverstab as d where d.account like :account";
				Query result=driverstabDAO.getSession().createQuery(hql);
				result.setString("account",reciveAccount);
				driversList=result.list();
				
				if(driversList.size()>=1)
				{
					try {
						resultJson.put("registResult","fail");
						resultJson.put("errorPoint", "accountUsed");
					} catch (JSONException e) {
						
						e.printStackTrace();
					}

				}
				else
				{
					Driverstab driverstab =new Driverstab();
					driverstab.setAccount(reciveAccount);
					driverstab.setPwd(recivePwd);
					driverstab.setLicence(reciveLicence);
					driverstab.setName(reciveName);
					//driverstab.setTelNumber(Integer.valueOf(reciveTel));
					driverstab.setTelNumber(reciveTel);
					driverstab.setEmail(reciveEmail);
					driverstabDAO.attachDirty(driverstab);
					driverstabDAO.getSession().getTransaction().commit();
					
					try {
						resultJson.put("registResult","succeed");
						resultJson.put("errorPoint", "none");
					} catch (JSONException e) {
						
						e.printStackTrace();
					}
				}
				
			}  
			catch(HibernateException e)
			{
				System.out.println("hibernate error in RegisServlet");
				e.printStackTrace();
			}
			finally
			{
				
				driverstabDAO.getSession().close();

			}
		}

		
		
		

		
		OutputStream out=response.getOutputStream();

		//		for (Driverstab driverstab : driversList) {
		//			out.write(driverstab.getAccount().toString().getBytes("utf-8"));
		//			out.write("\n".getBytes("utf-8"));
		//			out.write(driverstab.getPwd().toString().getBytes("utf-8"));
		//			
		//		}
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	}

}
